<?php
declare (strict_types = 1);

namespace app\common\controller;

use app\BaseController;
use app\common\service\ToolsService;
use app\common\service\SerialNumberService;
use think\helper\Str;

/**
 * 基类控制器
 * @author 贾二小
 * @since 2021/3/27
 */
class CommonBase extends BaseController
{

    /* 日志信息 */
    protected $logName = "";
    protected $logMsg = "";
    protected $logAccount = "";

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        //初始化请求配置参数
        self::initRequestConfig();
        ///登录用户
        $this->logAccount = _current_user('account');
        if(empty($this->logAccount)){
            $this->logAccount = '';
        }
    }

    /**
     * 初始化请求配置
     * @since 2021/3/27
     * @author 贾二小
     */
    private function initRequestConfig()
    {
        // 定义是否GET请求
        defined('IS_GET') or define('IS_GET', $this->request->isGet());

        // 定义是否POST请求
        defined('IS_POST') or define('IS_POST', $this->request->isPost());

        // 定义是否AJAX请求
        defined('IS_AJAX') or define('IS_AJAX', $this->request->isAjax());

        // 定义是否PAJAX请求
        defined('IS_PJAX') or define('IS_PJAX', $this->request->isPjax());

        // 定义是否PUT请求
        defined('IS_PUT') or define('IS_PUT', $this->request->isPut());

        // 定义是否DELETE请求
        defined('IS_DELETE') or define('IS_DELETE', $this->request->isDelete());

        // 定义是否HEAD请求
        defined('IS_HEAD') or define('IS_HEAD', $this->request->isHead());

        // 定义是否PATCH请求
        defined('IS_PATCH') or define('IS_PATCH', $this->request->isPatch());

        // 定义是否为手机访问
        defined('IS_MOBILE') or define('IS_MOBILE', $this->request->isMobile());

        // 定义是否为cli
        defined('IS_CLI') or define('IS_CLI', $this->request->isCli());

        // 定义是否为cgi
        defined('IS_CGI') or define('IS_CGI', $this->request->isCgi());

        // 定义应用名
        defined('APP_NAME') or define('APP_NAME', strtolower(App('http')->getName()));

        // 定义控制器名
        defined('CONTROLLER_NAME') or define('CONTROLLER_NAME', Str::snake($this->request->controller()));

        // 定义操作方法名
        defined('ACTION_NAME') or define('ACTION_NAME', strtolower($this->request->action()));

    }

    /**
     * 返回操作成功的请求
     * @param mixed $data 返回数据
     * @param integer $count 数量
     * @param string $msg 消息内容
     * @param integer $code 返回代码
     * @author 贾二小
     * @since 2021/8/2
     */
    protected function success($data=[], int $code = 200, string $msg="操作成功")
    {
        ToolsService::success($data, $code, $msg, $this->logName, $this->logMsg, $this->logAccount);
    }

    /**
     * 返回操作失败的请求
     * @param string $msg 消息内容
     * @param integer $code 返回代码
     * @author 贾二小
     * @since 2021/8/2
     */
    protected function error(string $msg, int $code = 201){
        ToolsService::success([], $code, $msg, $this->logName, $this->logMsg, $this->logAccount);
    }

    /**
     * 重定向
     * @author 贾二小
     * @since 2021/8/2
     */
    protected function redirect($url){
        ToolsService::redirect($url, $this->logName, $this->logMsg, $this->logAccount);
    }

    /**
     * 生成流水号
     * @param $serial_number_alias
     * @return mixed
     * @author 贾二小
     * @since 2021/3/28
     */
    protected function get_serial_number($serial_number_alias){
        return SerialNumberService::getSerialNumberByNo($serial_number_alias);
    }

    /**
     * 记录日志
     * @author 贾二小
     * @since 2021/8/3
     */
    protected function sLog($name, $msg, $account=''){
        $this->logName = $name;
        $this->logMsg = $msg;
        if(!empty($account)){
            $this->logAccount = $account;
        }
    }

}